_package("caf.ui");

_import("caf.ui.MessageBox");

/**
 * 对话框组件
 */
_class("DialogueBox", MessageBox, function(){
	var tpl = '<div class="{$cls}">'
		+   '<div class="mbox">'
		+     '<div class="title">{$title}</div>'
		+     '<div class="message">'
		+       '<p>{$msg}</p>'
		+       '<button class="mes-button sure-btn" _action="dlg_ok">确定</button>'
		+       '<button class="mes-button cancel-btn" _action="dlg_cancel">取消</button>'
		+     '</div>'
		+   '</div>'
		+ '</div>';
	this._init = function(title, message, agent, func){
		_super._init.call(this, title, message);
		this._params = {
			"title": title,
			"msg"  : message
		};
		this._cbid = agent ? runtime._task.add(agent, func) : 0;
	};
	this.create = function(parent, app, params){
		//_super.create.apply(this, arguments);
		this.setParent(parent);
		this.setApp(app);
		if(params) this.setParams(params);
		this.setId(this._id);
		var hash = {
			"cls"  : "caf-dialoguebox",
			"title": this._params.title,
			"msg"  : this._params.msg
		};
		var obj = this.createDomElement(parent, runtime.formatTpl(tpl, hash));
		this.init(obj);
		return obj;
	};
	this.init = function(obj){
		_super.init.apply(this, arguments);
		//this._button = this.find(".sure-btn");
		//this.addListener(this._button, "tap", this, "do_dlg_ok");
		this.addListener(this.find(".cancel-btn"), "tap", this, "do_dlg_cancel");
	};
	this.dispose = function(){
		_super.dispose.apply(this);
	};
	this.hide = function(){
		this.setVisible(false);
		/*
		var parent = this._parent.getContainer();
		var nodes = parent.childNodes;
		var a = [];
		for(var i = 0, len = nodes.length; i < len; i++){
			a.push(nodes[i]);
		}
		nodes = a;
		for(var i = 0, len = nodes.length; i < len; i++){
			var node = nodes[i];
			if(node.className == "caf-dialoguebox"){
				parent.removeChild(node);
			}
		}
		//if(parent.childNodes[i].className == "caf-dialoguebox"){
		//	parent.removeChild(parent.childNodes[i]);
		//}
		*/
	};
	this.customContent = function(htmlstr){
		this._self.innerHTML = htmlstr;
	};
	this.do_dlg_cancel = function(/*act, sender*/){
		var act = "dlg_cancel";
		this.hide();
		runtime._task.execute(this._cbid, [this, act]);
	};
});